package cn.ryh.ribbonproviderpayone.payment.controller;

import cn.ryh.apicommons.entities.CommonResult;
import cn.ryh.apicommons.entities.Payment;
import cn.ryh.ribbonproviderpayone.payment.service.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @ClassName PaymentController
 * @Description TODO
 * @Author RaoYH
 * @CreateDate 2020/11/20
 * @UpdateUser RaoYH
 * @UpdateDate 2020/11/20
 * @UpdateRemark TODO
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/payment")
public class PaymentController {
    private static final Logger log = LoggerFactory.getLogger(PaymentController.class);
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody Payment payment) {
        int i = paymentService.create(payment);
        log.info("》》》》》插入结果:" + i);
        if (i > 0) {
            return new CommonResult<>(200, "插入数据库成功,port:" + port, payment);
        } else {
            return new CommonResult<>(444, "插入数据库失败");
        }
    }

    /**
     * 对应访问url /payment/get/=1
     */
    @GetMapping(value = "/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("》》》》》查询结果:111" + payment);
        if (payment != null) {
            return new CommonResult<>(200, "查询成功,port:" + port, payment);
        } else {
            return new CommonResult<>(444, "没有对应记录，查询Id" + id);
        }
    }
    @RequestMapping(value = "/getServer")
    public String paymentZk() {
        return "Spring cloud with ribbon " + port +" "+ UUID.randomUUID().toString();
    }

}
